
# Copyright 2009 David Lee Miller
#
# This file is part of POSSM (Python Open Source Scanning Microscopy).
#
# POSSM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POSSM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License         
# along with POSSM.  If not, see <http://www.gnu.org/licenses/>.
#                                                                           
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

from client import ws,wx


TIMER_ID = 101

class ManualApproach(wx.Frame):
	''' Creates the frame and notebook for the created panels. '''
	def __init__(self, parent, name):
		wx.Frame.__init__(self, parent, -1, name, style=wx.DEFAULT_FRAME_STYLE)
		#self.timer = wx.Timer(self, id=102)
		#self.Bind(wx.EVT_TIMER, self.OnTimer, id=102)
		self.auxq = parent.auxq
		self.cmdq = parent.cmdq
		self._buttons()
		self._properties()
		self._layout()
		self.Show()
	
	def _layout(self):
		sizer = wx.FlexGridSizer(1,2,0,0)
		sizer2 = wx.FlexGridSizer(3,3,0,0)
		sizer3 = wx.FlexGridSizer(1,1,0,0)
		
		pan1 = wx.Panel(self,-1)
		pan2 = wx.Panel(self,-1)
		pan3 = wx.Panel(self,-1)
		pan4 = wx.Panel(self,-1)
		pan5 = wx.Panel(self,-1)

		direc = wx.StaticBox(self, -1, 'Direction')
		direcsizer = wx.BoxSizer(wx.HORIZONTAL)
		sizer2a = wx.StaticBoxSizer(direc,wx.VERTICAL)

		sizer2.Add(pan1,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(self.forward,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(pan2,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(self.left,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(pan3,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(self.right,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(pan4,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(self.back,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2.Add(pan5,0,wx.CENTER|wx.ADJUST_MINSIZE|wx.ALL,5)
		sizer2a.Add(sizer2, 0, wx.ADJUST_MINSIZE|wx.ALL|wx.CENTER, 5)

		rate = wx.StaticBox(self, -1, 'Rate')
		ratesizer = wx.BoxSizer(wx.HORIZONTAL)
		sizer3a = wx.StaticBoxSizer(rate,wx.VERTICAL)
		sizer3a.Add(self.rate, 0, wx.ADJUST_MINSIZE|wx.ALL|wx.CENTER, 5)
		sizer3.Add(sizer3a, 0, wx.ADJUST_MINSIZE|wx.ALL|wx.CENTER, 5)

		sizer.Add(sizer2a, 0, wx.ADJUST_MINSIZE|wx.ALL|wx.CENTER, 5)
		sizer.Add(sizer3)
		self.SetSizer(sizer)
		self.Fit()

	def _properties(self):
		self.rate.SetMinSize((130,27))
		self.forward.SetMinSize((35,35))
		self.back.SetMinSize((35,35))
		self.right.SetMinSize((35,35))
		self.left.SetMinSize((35,35))

	def _buttons(self):
		self.rate = wx.Slider(self,-1,size = (300,-1))
		self.forward = wx.Button(self, -1, 'F')
		self.back = wx.Button(self, -1, 'B')
		self.left = wx.Button(self, -1, 'L')
		self.left.Disable()
		self.right = wx.Button(self, -1, 'R')
		self.right.Disable()
		#self.cw = wx.Button(self, -1, 'CW')
		#self.ccw = wx.Button(self, -1, 'CCW')
		self.forward.Bind(wx.EVT_LEFT_DOWN, self.OnForward)
		self.forward.Bind(wx.EVT_LEFT_UP, self.OnStop)
		self.back.Bind(wx.EVT_LEFT_DOWN, self.OnBack)
		self.back.Bind(wx.EVT_LEFT_UP, self.OnStop)
	
	def OnForward(self,event):
		rate='%0.3d'%self.rate.GetValue()
		self.auxq.put(ws.CM_FORWARD+rate)
	
	def OnBack(self,event):
		rate='%0.3d'%self.rate.GetValue()
		self.auxq.put(ws.CM_BACK+rate)

	#def OnTimer(self,event):
		#self.auxq.put('GO GO GO')
		#event.Skip()

	def OnStop(self,event):
		self.auxq.put('stop')

